package com.example.demo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;

public class CodeGenerator {
    public static void main(String[] args) {
        AutoGenerator autoGenerator = new AutoGenerator();

        autoGenerator.setDataSource(dataSourceConfig());

        autoGenerator.setGlobalConfig(globalConfig());

        autoGenerator.setPackageInfo(packageConfig());

        autoGenerator.execute();
    }

    private static StrategyConfig strategyConfig(){
        StrategyConfig strategyConfig = new StrategyConfig();

        strategyConfig().setInclude("commodity","supplier");

        strategyConfig.setRestControllerStyle(true);

        strategyConfig.setEntityLombokModel(true);
        return strategyConfig;
    }

    private static PackageConfig packageConfig(){
        PackageConfig packageInfo = new PackageConfig();
        packageInfo.setParent("com.example.demo");
        packageInfo.setEntity("domain");
        packageInfo.setMapper("dao");
        return packageInfo;
    }

    private static DataSourceConfig dataSourceConfig(){
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/commodity?serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }

    private static GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        globalConfig.setOutputDir(projectPath + "\\src\\main\\java");//设置代码生成路径

        globalConfig.setOpen(false);

        globalConfig.setFileOverride(true);

        globalConfig.setMapperName("%sDao");

        globalConfig.setIdType(IdType.AUTO);
        return globalConfig;
    }
}
